//! HTML模板共享模块，用于消除重复的HTML代码

/// 生成手机端重定向HTML
use urlencoding::encode;

pub fn mobile_redirect_html(redirect_url: &str) -> String {
    // URL encode the redirect URL to ensure valid query string
    let encoded_url = encode(redirect_url);

    // HTML escape the encoded URL for safe embedding in HTML
    let escaped_url = encoded_url
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#x27;");
    
    format!(
        r#"<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="refresh" content="0; url={}">
    <title>你画我猜 - 手机端</title>
</head>
<body>
    <p>正在跳转到你画我猜手机端页面...</p>
    <p>重定向URL（调试用）: <code>{}</code></p>
    <script>
        window.location.href = '{}';
    </script>
</body>
</html>"#,
        escaped_url, escaped_url, encoded_url
    )
}

/// 生成TV端重定向HTML
pub fn tv_redirect_html(redirect_url: &str) -> String {
    format!(
        r#"<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>你画我猜 - TV端</title>
    <script>
        // 自动跳转到绘图页面
        window.location.href = '{}';
    </script>
</head>
<body>
    <div style="text-align: center; padding: 50px; font-family: Arial, sans-serif;">
        <h1>正在跳转到绘图游戏...</h1>
        <p>如果没有自动跳转，请<a href="{}">点击这里</a></p>
    </div>
</body>
</html>"#,
        redirect_url, redirect_url
    )
}
